//
//  SplashViewController.swift
//  test-ios
//
//  Created by wyz5204 on 2023/2/11.
//

import UIKit
import SnapKit
import ReactiveSwift
import ReactiveCocoa

class SplashViewController: ABaseViewController, UITableViewDataSource,UITableViewDelegate {
    
    static let MAIN1 = "主页"
    static let simple_list_view = "简单tableView列表"
    static let simple_image_table_view = "简单tableView图片列表"
    static let constraints = "约束布局 Controller"
    static let network = "网络 Controller"
    static let sql = "数据库 Controller"
    static let kv = "键值对 Controller"
    static let rx = "函数式Rxswift"
    static let dialog = "弹窗 Dialog"
    static let login = "荣大 科技 APP"
    static let leaks = "测试Leaks"
    static let dispatch = "测试 Dispatch"
    static let notification = "测试 Notification"
    static let location = "测试 定位Location"
    static let web = "测试 Web"
    static let file = "文件管理"
    static let MVVM = "MVVM 框架"
    static let GitHub = "GitHub MVVM"
    static let reflex = "反射 Swift"
    static let json = "Json学习"
    static let rdMain = "荣大Main"
    
    var router : [String] = [
        rdMain ,
        json ,
        reflex ,
        rx ,
        dialog ,
        GitHub ,
        MVVM ,
        file ,
        sql ,
        web ,
        login ,
        dispatch ,
        network ,
        location ,
        notification ,
        leaks ,
        MAIN1 ,
        simple_list_view,
        simple_image_table_view ,
        constraints ,
        kv ,
    ]
    
    @objc func receive(){
        print("接收到发送的消息")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        registerObserver(selector: #selector(receive), name: NotificationController.NSplash)
        // 使用列表
        let routerTableView = UITableView()
        routerTableView.dataSource = self
        routerTableView.delegate = self
        routerTableView.register(SplashTableViewCell.self, forCellReuseIdentifier: "textCell")
        routerTableView.addTo(self.view) { view in
            view.snp.makeConstraints { make in
                make.left.equalTo(self.view.snp.left)
                make.top.equalTo(self.view.snp.top)
                make.right.equalTo(self.view.snp.right)
                make.bottom.equalTo(self.view.snp.bottom)
            }
        }
        
        
        //debug
        #if DEBUG
        let debug = DebugViewHelper()
        debug.setDebug(DebugViewHelper.Callback(name: "debug", call: {
            Logger.e("test1 click")
        }),DebugViewHelper.Callback(name: "debug", call: {
            Logger.e("test1 click")
        }),DebugViewHelper.Callback(name: "debug", call: {
            Logger.e("test1 click")
        }),DebugViewHelper.Callback(name: "debug", call: {
            Logger.e("test1 click")
        }),DebugViewHelper.Callback(name: "debug", call: {
            Logger.e("test1 click")
        }),DebugViewHelper.Callback(name: "debug", call: {
            Logger.e("test1 click")
        }))
        
        debug.addTo(self.view) { view in
            view.snp.makeConstraints { make in
                make.centerY.equalToSuperview()
                make.right.equalToSuperview()
            }
        }
        #endif
        
        
    }
    // MARK: - 生命周期方法
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.router.count
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print(self.router[indexPath.row])
        switch(self.router[indexPath.row]){
        case Self.rdMain :
            startViewController(RDMainTabBarViewComtroller())
        case Self.json :
            startViewController(JsonViewController(viewModel : SimpleViewModel.shared))
        case Self.reflex :
            startViewController(ReflexSwiftViewController(viewModel : SimpleViewModel.shared))
        case Self.GitHub :
            MVVMNavigator.shared.show(segue: MVVMNavigator.Scene.github, sender: self)
        case Self.MVVM :
            MVVMNavigator.shared.show(segue: MVVMNavigator.Scene.webController(URL.init(string: "https://home.firefoxchina.cn/")!), sender: self)
        case Self.dialog :
            self.startViewController(DialogViewController(viewModel : DialogViewModel()))
        case Self.file :
            self.startViewController(FileManagerController())
        case Self.sql :
            self.startViewController(SQLController())
        case Self.web :
            self.startViewController(WebViewController())
        case Self.location :
            self.startViewController(LocationController())
        case Self.notification :
            self.startViewController(NotificationController())
        case Self.dispatch :
            self.startViewController(DispatchViewController())
        case Self.leaks :
            self.startViewController(LeakViewController())
        case Self.MAIN1 :
            self.startViewController(ListViewController())
        case Self.simple_image_table_view :
            self.startViewController(ImageListViewController())
        case Self.login:
            self.startViewController(LoginMVVMViewController(viewModel: LoginViewModel()))
        case Self.rx:
            self.startViewController(RxSwiftViewController())
        case Self.kv:
            self.startViewController(KVViewController())
        case Self.network:
            self.startViewController(NetWorkViewController())
        case Self.constraints:
            self.startViewController(ConstraintsViewController())
        default:
            print(self.router[indexPath.row])
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let textCell = tableView.dequeueReusableCell(withIdentifier: "textCell", for: indexPath) as! SplashTableViewCell
        textCell.initBinder(tableView , self.router[indexPath.row] , indexPath)
        return textCell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        50
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        post {
            self.startViewController(LoginMVVMViewController(viewModel: LoginViewModel()))
        }
    }
}


